//
//  YWTailorRangeView.m
//  YWPhotoEditMaster
//
//  Created by jun peng on 2023/8/23.
//

#import "YWTailorRangeView.h"

@interface YWTailorRangeView ()
@property (weak, nonatomic) IBOutlet UIView *tailRangeView;
@property (weak, nonatomic) IBOutlet UIImageView *letTopView;
@property (weak, nonatomic) IBOutlet UIImageView *leftBottomView;
@property (weak, nonatomic) IBOutlet UIImageView *rightTopView;
@property (weak, nonatomic) IBOutlet UIImageView *rightBottomView;
@property (weak, nonatomic) IBOutlet UIImageView *leftShuView;
@property (weak, nonatomic) IBOutlet UIImageView *rightShuView;
@property (weak, nonatomic) IBOutlet UIImageView *topHengView;
@property (weak, nonatomic) IBOutlet UIImageView *bottomHengView;


@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tailRangeViewTopConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tailRangeViewLeadingConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tailRangeViewBottomConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tailRangeViewTrailingConstraint;



@end


@implementation YWTailorRangeView


+(instancetype)tailorRangeView{
    
    return [[NSBundle mainBundle] loadNibNamed:@"YWTailorRangeView" owner:nil options:nil].lastObject;
}

-(void)awakeFromNib{
    
    [super awakeFromNib];
    
    self.tailRangeView.tag = 1;
    self.letTopView.tag = 2;
    self.leftBottomView.tag = 3;
    self.rightTopView.tag = 4;
    self.rightBottomView.tag = 5;
    self.leftShuView.tag = 6;
    self.rightShuView.tag = 7;
    self.topHengView.tag = 8;
    self.bottomHengView.tag = 9;
    
    
    
    UIPanGestureRecognizer *pan1 = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    [self.tailRangeView addGestureRecognizer:pan1];
    
    UIPanGestureRecognizer *pan2 = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    [self.letTopView addGestureRecognizer:pan2];
    
    UIPanGestureRecognizer *pan3 = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    [self.leftBottomView addGestureRecognizer:pan3];
    
    UIPanGestureRecognizer *pan4 = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    [self.rightTopView addGestureRecognizer:pan4];
    
    UIPanGestureRecognizer *pan5 = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    [self.rightBottomView addGestureRecognizer:pan5];
    
    UIPanGestureRecognizer *pan6 = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    [self.leftShuView addGestureRecognizer:pan6];
    
    UIPanGestureRecognizer *pan7 = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    [self.rightShuView addGestureRecognizer:pan7];
    
    UIPanGestureRecognizer *pan8 = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    [self.topHengView addGestureRecognizer:pan8];
    
    UIPanGestureRecognizer *pan9 = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    [self.bottomHengView addGestureRecognizer:pan9];
    
    
}

- (void)pan:(UIPanGestureRecognizer *)pan {
    
    CGPoint transP = [pan translationInView:pan.view];
    UIView *panView = pan.view;
    switch (panView.tag) {
        case 1: //tailRangeView
            {
                if (self.tailRangeViewLeadingConstraint.constant <= 0 && self.tailRangeViewTopConstraint.constant <= 0 && (transP.x <= 0 && transP.y <= 0)){ //贴着左上角、往左上走
                    
                    self.tailRangeViewLeadingConstraint.constant = 0;
                    self.tailRangeViewTopConstraint.constant = 0;
                    
                    
                }
                
                else if (self.tailRangeViewLeadingConstraint.constant <= 0 && self.tailRangeViewBottomConstraint.constant <= 0 && (transP.x <= 0 && transP.y >= 0)){ //贴着左下角、往左下走
                    self.tailRangeViewLeadingConstraint.constant = 0;
                    self.tailRangeViewBottomConstraint.constant = 0;
                    
                }
                
                else if (self.tailRangeViewTrailingConstraint.constant <= 0 && self.tailRangeViewTopConstraint.constant <= 0 && (transP.x >= 0 && transP.y <= 0)){ //贴着右上角、往右上走
                    self.tailRangeViewTrailingConstraint.constant = 0;
                    self.tailRangeViewTopConstraint.constant = 0;
                    
                }
                else if (self.tailRangeViewTrailingConstraint.constant <= 0 && self.tailRangeViewBottomConstraint.constant <= 0 && (transP.x >= 0 && transP.y >= 0)){ //贴着右下角、往右下走
                    self.tailRangeViewTrailingConstraint.constant = 0;
                    self.tailRangeViewBottomConstraint.constant = 0;
                    
                }
                else if (self.tailRangeViewLeadingConstraint.constant <= 0 && transP.x <= 0){ //贴着左边
                    
                    self.tailRangeViewLeadingConstraint.constant  = 0;
                    self.tailRangeViewTopConstraint.constant += transP.y;
                    self.tailRangeViewBottomConstraint.constant -= transP.y;
                }
                else if (self.tailRangeViewTrailingConstraint.constant <= 0 && transP.x >= 0){ //贴着右边
                    self.tailRangeViewTrailingConstraint.constant = 0;
                    self.tailRangeViewTopConstraint.constant += transP.y;
                    self.tailRangeViewBottomConstraint.constant -= transP.y;
                    
                }
                else if (self.tailRangeViewTopConstraint.constant <= 0 && transP.y <= 0){ //贴着上边
                    self.tailRangeViewTopConstraint.constant = 0;
                    self.tailRangeViewLeadingConstraint.constant += transP.x;
                    self.tailRangeViewTrailingConstraint.constant -= transP.x;
                    
                }
                else if (self.tailRangeViewBottomConstraint.constant <= 0 && transP.y >= 0){ //贴着下边
                    self.tailRangeViewBottomConstraint.constant = 0;
                    self.tailRangeViewLeadingConstraint.constant += transP.x;
                    self.tailRangeViewTrailingConstraint.constant -= transP.x;
                    
                }
                else {
                    
                    self.tailRangeViewLeadingConstraint.constant += transP.x;
                    self.tailRangeViewTrailingConstraint.constant -= transP.x;
                    self.tailRangeViewTopConstraint.constant += transP.y;
                    self.tailRangeViewBottomConstraint.constant -= transP.y;
                }
                
                
            }
            break;
        case 2: //letTopView
            {
                if(!(self.dealSize.width - self.tailRangeViewLeadingConstraint.constant - self.tailRangeViewTrailingConstraint.constant <= 60 && transP.x > 0)){
                    
                    
                    self.tailRangeViewLeadingConstraint.constant += transP.x;
                    
                    if(self.tailRangeViewLeadingConstraint.constant < 0){
                        self.tailRangeViewLeadingConstraint.constant = 0;
                    }
                }
                
                if(!(self.dealSize.height - self.tailRangeViewTopConstraint.constant - self.tailRangeViewBottomConstraint.constant <= 60 && transP.y > 0)){
                    
                    self.tailRangeViewTopConstraint.constant += transP.y;
                    
                    if(self.tailRangeViewTopConstraint.constant < 0){
                        self.tailRangeViewTopConstraint.constant = 0;
                    }
                }
                
                
                
                
            }
            break;
        case 3: //leftBottomView
            {
                if(!(self.dealSize.width - self.tailRangeViewLeadingConstraint.constant - self.tailRangeViewTrailingConstraint.constant <= 60 && transP.x > 0)){
                    
                    
                    self.tailRangeViewLeadingConstraint.constant += transP.x;
                    
                    if(self.tailRangeViewLeadingConstraint.constant < 0){
                        self.tailRangeViewLeadingConstraint.constant = 0;
                    }
                    
                    
                }
                
                if(!(self.dealSize.height - self.tailRangeViewTopConstraint.constant - self.tailRangeViewBottomConstraint.constant <= 60 && transP.y < 0)){
                    
                    self.tailRangeViewBottomConstraint.constant -= transP.y;
                    
                    if(self.tailRangeViewBottomConstraint.constant < 0){
                        self.tailRangeViewBottomConstraint.constant = 0;
                    }
                }
                
                
            }
            break;
        case 4: //rightTopView
            {
                if(!(self.dealSize.width - self.tailRangeViewLeadingConstraint.constant - self.tailRangeViewTrailingConstraint.constant <= 60 && transP.x < 0)){
                    
                    self.tailRangeViewTrailingConstraint.constant -= transP.x;
                    if(self.tailRangeViewTrailingConstraint.constant < 0){
                        self.tailRangeViewTrailingConstraint.constant = 0;
                    }
                }
                
                if(!(self.dealSize.height - self.tailRangeViewTopConstraint.constant - self.tailRangeViewBottomConstraint.constant <= 60 && transP.y > 0)){
                    
                    self.tailRangeViewTopConstraint.constant += transP.y;
                    if(self.tailRangeViewTopConstraint.constant < 0){
                        self.tailRangeViewTopConstraint.constant = 0;
                    }
                }
                
                
            }
            break;
        case 5: //rightBottomView
            {
                if(!(self.dealSize.width - self.tailRangeViewLeadingConstraint.constant - self.tailRangeViewTrailingConstraint.constant <= 60 && transP.x < 0)){
                    
                    self.tailRangeViewTrailingConstraint.constant -= transP.x;
                    if(self.tailRangeViewTrailingConstraint.constant < 0){
                        self.tailRangeViewTrailingConstraint.constant = 0;
                    }
                }
                
                if(!(self.dealSize.height - self.tailRangeViewTopConstraint.constant - self.tailRangeViewBottomConstraint.constant <= 60 && transP.y < 0)){
                    
                    self.tailRangeViewBottomConstraint.constant -= transP.y;
                    if(self.tailRangeViewBottomConstraint.constant < 0){
                        self.tailRangeViewBottomConstraint.constant = 0;
                    }
                }
                
                
            }
            break;
        case 6: //leftShuView
            {
                if(!(self.dealSize.width - self.tailRangeViewLeadingConstraint.constant - self.tailRangeViewTrailingConstraint.constant <= 60 && transP.x > 0)){
                    
                        self.tailRangeViewLeadingConstraint.constant += transP.x;
                    if(self.tailRangeViewLeadingConstraint.constant < 0){
                        self.tailRangeViewLeadingConstraint.constant = 0;
                    }
                }
                
                
            }
            break;
        case 7: //rightShuView
            {
                if(!(self.dealSize.width - self.tailRangeViewLeadingConstraint.constant - self.tailRangeViewTrailingConstraint.constant <= 60 && transP.x < 0)){
                    
                    self.tailRangeViewTrailingConstraint.constant -= transP.x;
                    if(self.tailRangeViewTrailingConstraint.constant < 0){
                        self.tailRangeViewTrailingConstraint.constant = 0;
                    }
                }
            }
            break;
        case 8: //topHengView
            {
                if(!(self.dealSize.height - self.tailRangeViewTopConstraint.constant - self.tailRangeViewBottomConstraint.constant <= 60 && transP.y > 0)){
                    
                    self.tailRangeViewTopConstraint.constant += transP.y;
                    if(self.tailRangeViewTopConstraint.constant < 0){
                        self.tailRangeViewTopConstraint.constant = 0;
                    }
                    
                }
                
            }
            break;
        case 9: //bottomHengView
            {
                if(!(self.dealSize.height - self.tailRangeViewTopConstraint.constant - self.tailRangeViewBottomConstraint.constant <= 60 && transP.y < 0)){
                    
                    self.tailRangeViewBottomConstraint.constant -= transP.y;
                    if(self.tailRangeViewBottomConstraint.constant < 0){
                        self.tailRangeViewBottomConstraint.constant = 0;
                    }
                    
                }
            }
            break;
            
        default:
            break;
    }
    
    
    //复位
    [pan setTranslation:CGPointZero inView:pan.view];
    
    NSLog(@"frame: %@",NSStringFromCGRect(self.tailRangeView.frame));
    
    if([self.delegate respondsToSelector:@selector(tailorRangeView:tailorRange:)]){
        [self.delegate tailorRangeView:self tailorRange:self.tailRangeView.frame];
    }
}


@end
